package cn.dengta.webapp.user.vo;

import java.util.Date;
import java.util.Map;

import cn.dengta.common.model.APIResponse.Field;
import cn.dengta.common.model.Status;
import cn.dengta.common.model.SwitchSupport;
import cn.dengta.common.util.Formats;
import cn.dengta.common.web.Client;
import cn.dengta.context.model.Language;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.base.vo.AbstractVO;
import cn.dengta.webapp.user.entity.Link;
import cn.dengta.webapp.user.entity.Link.LinkConfig;
import cn.dengta.webapp.user.model.*;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.X;

@Getter
@Setter
public class LinkVO extends AbstractVO<Link> {

	/** 别名 */
	@Field(name = "别名")
	protected String alias;
	/** 点击次数 */
	@Field(name = "点击次数")
	protected Integer hits;
	/** 站点描述 */
	@Field(name = "站点描述")
	protected String summary;
	/** 站点logo */
	@Field(name = "站点logo")
	protected String logo;
	/** 站点名称 */
	@Field(name = "站点名称")
	protected String name;
	/** 标签 */
	@Field(name = "标签")
	protected String tag;
	/** 站点排序 */
	@Field(name = "排序")
	protected Integer sort;
	/** 站点类型 */
	@Field(name = "站点类型")
	protected String type;
	/** 站点状态 */
	@Field(name = "站点状态")
	protected Integer status;
	/** 性别 */
	public Gender gender;
	/** 客户端 */
	protected String client;
	public Boolean canDelete;
	public Boolean canToggle;
	public String statusDesc;
	public Language language;
	public Global global;
	public Object assoc;
	public Integer linkCategory;
	/** 关联类型 */
	protected transient String assocType;
	/** 关联id（多个ID以逗号隔开） */
	protected String assocIds;
	/** 分包 */
	private String wesKits;

	/** 站点路径 */
	protected transient String siteUrl;
	/** 开启时间 */
	protected transient Date beginTime;
	protected transient Date endTime;
	protected Map<String, Object> addUser;
	protected transient LinkConfig resolvedCfg;

	public static LinkVO from(Link source) {
		return copy(source, LinkVO::new);
	}

	public static LinkVO fromFront(Link source) {
		return copy(source, LinkVO::new, "hits", "sort", "addUser", "alias", "tag");
	}

	public static LinkVO fromBanner(Link source) {
		return copy(source, LinkVO::new, "addUser", "hits", "sort");
	}

	public String getSiteUrl_() {
		return siteUrl;
	}

	public String getBeginTime_() {
		return Formats.formatDatetime(beginTime);
	}

	public String getEndTime_() {
		return Formats.formatDatetime(endTime);
	}

	public Integer getGender() {
		return X.map(gender, Gender::getValue);
	}

	public String getGender_() {
		return X.map(gender, Gender::getLabel);
	}

	public String getGlobal_() {
		return X.map(global, Global::getLabel);
	}

	public String getClient_() {
		return X.map(Client.of(client), Client::getLabel);
	}

	public String getType_() {
		return X.map(LinkType.of(type), LinkType::getLabel);
	}

	public Integer getLanguage() {
		return X.map(language, Language::getValue);
	}

	public String getLanguage_() {
		return X.map(language, Language::getLabel);
	}

	public Boolean isOpen(Long currentTimeMillis) {
		if (currentTimeMillis == null) {
			currentTimeMillis = now().getTime();
		}
		return Status.asResult(status) && SwitchSupport.between(currentTimeMillis, beginTime, endTime);
	}

	public LinkConfig getConfig() {
		return resolvedCfg;
	}

	public String getWesKits_() {
		return WesKit.getWesKits_(wesKits);
	}

}
